﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using eProject_SEM3_SecurityServices.Bussiness;
using eProject_SEM3_SecurityServices.ObjectClass;
using System.Data.SqlClient;
using System.Data;

namespace eProject_SEM3_SecurityServices.DataAccess
{
    public class DDepartment
    {
        public bool AddNewDepartment(ODepartment department)
        {
            SqlParameter[] parameter = new SqlParameter[]
            {
                new SqlParameter("@DepId", department.DepartmentID),
                new SqlParameter("@Name", department.DepartmentName),
                new SqlParameter("@Description", department.DepartmentDescription)                
            };

            return DConnectSQL.ExecuteNonQuery("AddNewDepartment",CommandType.StoredProcedure, parameter);
        }
        
        public bool UpdateDepartment(ODepartment department)
        {
            SqlParameter[] parameter = new SqlParameter[]
            {
                new SqlParameter("@DepId", department.DepartmentID),
                new SqlParameter("@Name", department.DepartmentName),
                new SqlParameter("@Description", department.DepartmentDescription)
                
            };

            return DConnectSQL.ExecuteNonQuery("UpdateDepartment",CommandType.StoredProcedure, parameter);
        }

        public bool DeleteDepartment(int DepID)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@depId", DepID)
            };

            return DConnectSQL.ExecuteNonQuery("DeleteDepartment", CommandType.StoredProcedure, parameters);
        }

        public ODepartment GetDepartmentDetails(int depID)
        {
            ODepartment department = null;

            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@depID", depID)
            };
            //Lets get the list of all employees in a datataable
            using (DataTable table = DConnectSQL.ExecuteParamerizedSelectCommand("GetDepartmentDetails", CommandType.StoredProcedure, parameters))
            {
                //check if any record exist or not
                if (table.Rows.Count == 1)
                {
                    DataRow row = table.Rows[0];

                    //Lets go ahead and create the list of employees
                    department = new ODepartment();

                    //Now lets populate the employee details into the list of employees                                           
                    department.DepartmentID = Convert.ToInt32(row["departmentId"]);
                    department.DepartmentName = row["departmentName"].ToString();
                    department.DepartmentDescription = row["departmentDesciption"].ToString();
                    
                }
            }

            return department;
        }


        public List<ODepartment> GetDepartmentList()
        {
            List<ODepartment> listDepartment = null;

            //Lets get the list of all employees in a datataable
            using (DataTable table = DConnectSQL.ExecuteSelectCommand("GetDepartmentList", CommandType.StoredProcedure))
            {
                //check if any record exist or not
                if (table.Rows.Count > 0)
                {
                    //Lets go ahead and create the list of employees
                    listDepartment = new List<ODepartment>();

                    //Now lets populate the employee details into the list of employees
                    foreach (DataRow row in table.Rows)
                    {
                        ODepartment department = new ODepartment();
                        department.DepartmentID = Convert.ToInt32(row["departmentId"]);
                        department.DepartmentName = row["departmentName"].ToString();
                        department.DepartmentDescription = row["departmentDesciption"].ToString();
                        

                        listDepartment.Add(department);
                    }
                }
            }

            return listDepartment;
        }    
    }
}